package com.xiaren.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getCookies();
		request.getSession().setAttribute("user", "sssssssss");
		
		String user = (String)request.getSession().getAttribute("user");
		response.getWriter().append(user);
		response.getWriter().append("\n");
		
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie : cookies){
			response.getWriter().append("cookie.name = " + cookie.getName() + ", cookie.value = " + cookie.getValue() + "\n");
		}
		Cookie cookie = new Cookie("cookiename1","cookievalue");
		cookie.setMaxAge(50);
		cookie.setPath("/");
		response.addCookie(cookie);
		cookie = new Cookie("cookiename2","cookievalue");
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	public static void main(String[] args) {
		int i = 071;
		System.out.println(i);
	}
}
